// pages/pfep/safeguard_select/safeguard_select.js
const mybehavior = require("../../../behaviors/requests")

Page({

    /**
     * 页面的初始数据
     */
    behaviors: [mybehavior],
    data: {
        baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
        leibie: "",
        popup_show1: false,
        popup_show2: false,
        options1: ['来料包装库'],
        radio: '',
        checkbox_list1: [

        ],
        checkbox_value1: "",
        checkbox_list2: [

        ],
        checkbox_value2: "",




    },
    // 点击标准类别选取输入框
    onClick11(event) {
        // event.detail 为当前输入的值
        // console.log(event.detail);
        this.setData({
            popup_show1: true
        })
    },
    // 选择标准类别选取输入框关闭
    onClose11() {
        this.setData({ popup_show1: false });
    },
    // 确认标准类别选取值--弹出框关闭
    onPopupChange1(event) {
        const { picker, value, index } = event.detail;
        this.setData({
            leibie: value,
            popup_show1: false,
        })
        this.getTemplateDatas()
        this.getStandardDatas()
    },
    // 选择模板方式
    onChange(event) {
        this.setData({
            radio: event.detail,
        });
        let key = event.detail
        if (key == '1') {
            this.setData({
                popup_show2: true,
            })
        } else if (key == '2') {
            this.setData({
                popup_show3: true,
            })
        } else {

        }
    },
    submit() {
        if (this.data.radio == "3") {
            console.log(this.data.radio);
            // wx.navigateTo({
            //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
            // })
            wx.navigateTo({
                url: '/pages/pfep/MaintenanceTemplate/Template3/Template3?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_num,
                // success(res) {
                //     res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

                // }
            })

        }
    },
    // 筛选现有包装模板数据
    bindInput1(e) {
        console.log(e.detail);

    },
    // 获取--现有包装模板--数据
    getTemplateDatas(e) {
        console.log(e);
        let query_cond = {}
        if (e !== undefined) {
            if (e.detail.indexOf("*") > -1) {
                let arr = e.detail.split("*")
                let str = arr.join("\\*")
                query_cond = {
                    name: { "$regex": str }
                }
            } else {
                query_cond = {
                    name: { "$regex": e.detail }
                }
            }

        }
        let url = this.data.baseUrl + "/template_manage/query_templates"
        let data = {
            page: 1,
            page_size: 99999,
            query_cond: query_cond
        }
        this.post(url, data).then(res => {
            console.log("现有包装模板", res.data.results);

            if (res.code == 20000) {

                this.setData({
                    checkbox_list1: res.data.results,

                })

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        }).catch(err => {
            console.log(err);
            wx.showToast({
                title: err.msg,
                icon: 'error'
            })
        })
    },
    query_cond: {
        "content.database": "辅材库"
    },
    // 获取--物料编码模板--数据
    getStandardDatas(e) {
        console.log(e);
        let query_cond = {}
        if (e !== undefined) {
            console.log("xxx");
            query_cond = {
                "content.infoData.material_code": e.detail
            }

        }
        let url = this.data.baseUrl + "/standard_doc_store_manage/query_standard_doc_store"
        let data = {
            page: 1,
            page_size: 99999,
            query_cond: query_cond,
            tb_key: ""
        }
        console.log("afsdf", data);
        this.post(url, data).then(res => {
            console.log("物料编码模板", res.data.results);

            if (res.code == 20000) {

                this.setData({
                    checkbox_list2: res.data.results,

                })

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        }).catch(err => {
            console.log(err);
            wx.showToast({
                title: err.msg,
                icon: 'error'
            })
        })
    },


    // 现有包装模板值选中
    onPopupChange2(event) {
        console.log(event);
        this.setData({
            checkbox_value1: event.detail,
            // popup_show2: false,
        })


    },
    onClose2() {
        this.setData({ popup_show2: false });
    },
    // 预览包装信息
    previewBtn1(e) {
        console.log(e.currentTarget.dataset.row_data);
        // wx.navigateTo({
        //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
        // })
        wx.navigateTo({
            url: '/pages/pfep/MaintenanceTemplate/previewPage/previewPage1/previewPage1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_num,
            success(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

            }
        })
    },



    // 现有物料编码标准值选中
    onPopupChange3(event) {
        this.setData({
            checkbox_value2: event.detail,
            // popup_show2: false,
        })
    },
    onClose3() {
        this.setData({ popup_show3: false });
    },
    toTemplate1(e) {
        console.log(e.currentTarget.dataset.row_data);
        // wx.navigateTo({
        //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
        // })
        wx.navigateTo({
            url: '/pages/pfep/MaintenanceTemplate/Template1/Template1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_num,
            success(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

            }
        })
    },
    toTemplate2(e) {
        console.log(e.currentTarget.dataset.row_data);
        wx.navigateTo({
            url: '/pages/pfep/MaintenanceTemplate/Template1/Template1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_num,
            success(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

            }
        })

    },
    toTemplate3() {
        wx.navigateTo({
            url: '/pages/pfep/MaintenanceTemplate/Template2/Template3',
        })

    },
    // 预览物料编码信息
    previewBtn2() { },

    // 获取--物料组、物料分类码--数据
    getMaterialCodeDatas(val) {
        let url = this.data.baseUrl + "/standard_doc_manage/query_material_num_info"
        let data = {
            material_num: val
        }
        this.post(url, data).then(res => {
            // let data_list = Object.keys(res.data)

            if (res.code == 20000) {

                this.setData({
                    citg: res.data.citg,
                    cpcl: res.data.cpcl,
                    material_num: val

                })

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        }).catch(err => {
            console.log(err);
            wx.showToast({
                title: err.msg,
                icon: 'error'
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options.material_num);
        this.getMaterialCodeDatas(options.material_num)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})